package com.mtons.mblog.modules.service.helper;

import com.mtons.mblog.base.utils.RedisHelper;
import com.mtons.mblog.modules.pojo.User;
import com.mtons.mblog.modules.service.UserService;
import com.mtons.mblog.modules.vo.AccountProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName: RedisFollowHelper
 * @Auther: Jerry
 * @Date: 2020/5/21 16:27
 * @Desctiption: TODO
 * @Version: 1.0
 */
@Component
public class RedisFollowHelper {

    //爱豆（FOLLOW_USERS） 的粉丝表
    private static final String FOLLOW_USERS = "USER_%s:myFans";
    //脑残粉（FANS_USERS） 的关注表
    private static final String FANS_USERS = "USER_%s:myFollows";

    @Autowired
    UserService userService;
    @Autowired
    private RedisHelper redisHelper;

    //userId去关注别人，判断是否已关注
    public boolean isFollowed(Long followId, AccountProfile profile) {

        String fanUsersKey = String.format(FANS_USERS, profile.getId());
        Object value = redisHelper.hget(fanUsersKey, String.valueOf(followId));
        return value != null ;
    }
    //followId去关注别人,判断是否已关注
    public boolean isFollowedByFollowId(Long followId, AccountProfile profile) {

        String fanUsersKey = String.format(FANS_USERS, followId);
        Object value = redisHelper.hget(fanUsersKey, String.valueOf(profile.getId()));
        return value != null ;
    }

    public void follow(Long followId, AccountProfile profile) {
        User followUser = userService.getByUserId(followId);
        //粉丝的爱豆们
        String followUsersKey = String.format(FOLLOW_USERS, followId);
        redisHelper.hset(followUsersKey, String.valueOf(profile.getId()), profile.getUsername());

        //爱豆的粉丝们
        String fanUsersKey = String.format(FANS_USERS, profile.getId());
        redisHelper.hset(fanUsersKey, String.valueOf(followId), followUser.getUsername());
    }

    public void unFollow(Long followId, AccountProfile profile) {
        User followUser = userService.getByUserId(followId);
        //粉丝的爱豆们
        String followUsersKey = String.format(FOLLOW_USERS, followId);
        redisHelper.hdel(followUsersKey, String.valueOf(profile.getId()), profile.getUsername());

        //爱豆的粉丝们
        String fanUsersKey = String.format(FANS_USERS, profile.getId());
        redisHelper.hdel(fanUsersKey, String.valueOf(followId), followUser.getUsername());
    }

    public Map<Object,Object> myFollows(Long userId){
        String fansUsersKey = String.format(FANS_USERS,userId);
        Map<Object,Object> objectMap = redisHelper.getHash(fansUsersKey);

        return objectMap;
    }
    public Map<Object,Object> myFans(Long followId){
        String followUsersKey = String.format(FOLLOW_USERS, followId);
        Map<Object,Object> objectMap = redisHelper.getHash(followUsersKey);
        return objectMap;
    }

    public Long  myFollowCount(Long userId){
        String fansCountKey = String.format(FANS_USERS, userId);
        Long fansCount= redisHelper.hLen(fansCountKey);
        return  fansCount;
    }

    public Long myFansCount(Long followId){
        String followCountKey = String.format(FOLLOW_USERS, followId);
        Long followCount= redisHelper.hLen(followCountKey);
        return  followCount;
    }

}
